// pages/home/device/setreceiver.js
var app = getApp();
var page_did = "";
var page_call=1;
var url = app.globalData.Url +"image/wx.jpg";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgalist: [url],
    receiver_wx: "",
    receiver_mail: "",
    receiver_type: 1,//1:邮箱;2:微信
    request_type: 0,
    response_result: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    page_call=1;
    page_did = options.did;
  },
  listerner_wx: function (e) {
    this.setData({
      receiver_wx: e.detail.value
    })
  },
  listerner_mail: function (e) {
    this.setData({
      receiver_mail: e.detail.value
    })
  },
  previewImage: function (e) {
    var current = e.target.dataset.src
    console.log(this.data.imgalist);
    wx.previewImage({
      current: current, // 当前显示图片的http链接     
      urls: this.data.imgalist // 需要预览的图片http链接列表     
    })
    wx.getImageInfo({// 获取图片信息（此处可不要）  
      src: url,
      success: function (res) {
        //console.log(res.width)
        //console.log(res.height)
      }
    })
    win
  },
  //添加接收人
  onsavereceiver: function (e) {
    var that = this;
    this.data.request_type = 0;
    if (that.data.receiver_mail != "") { this.data.request_type = this.data.request_type + 1; }
    if (that.data.receiver_wx != "") { this.data.request_type = this.data.request_type + 2; }

    switch (this.data.request_type) {
      case 3:
      case 1: {
        that.data.receiver_type = 1;
        this.setreceiver();
        break;
      }
      case 2: {
        that.data.receiver_type = 2;
        this.setreceiver();
        break;
      }
      default:{
        wx.showToast({
          title: "请填写微信帐号或邮箱",
          icon: 'none',
          duration: 1000
        })
        break;
      }
    }
    //console.log(this.data.request_type);
  },
  setreceiver: function (e) {
    var that = this;
    var _type = that.data.receiver_type;
    var _user = _type == 1 ? that.data.receiver_mail : that.data.receiver_wx;
    var token = app.globalData.Token;
    if (token == null || token == "") return;
    wx.request({
      url: app.globalData.Url + "Api/Device/SetMessageReceiver", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        did: page_did,
        type: _type,
        user: _user,
        call:page_call,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        //console.log(res.data);
        if (res.data.result) {
          var rt = that.data.receiver_type;
          that.data.response_result=that.data.response_result + rt;
          //console.log("rt:" + rt + " rs:" + that.data.response_result + " c:" + that.data.request_type);
          if (that.data.response_result == that.data.request_type) {
            wx.navigateBack();
          }else{
            that.data.receiver_type = 2;
            that.setreceiver();
          }
        }
        else {
          wx.showToast({
            title: res.data.data,
            icon: 'none',
            duration: 1000
          })
          if (that.data.receiver_type=2)
          {
            that.onfollow();
          }
        }
      }
    })
  },
  //更新微信用户
  onfollow: function (e) {
    var that = this;
    var token = app.globalData.Token;
    if (token == null || token == "") return;
    wx.request({
      url: app.globalData.Url + "Api/User/FollowWeiXin", //仅为示例，并非真实的接口地址
      data: {
        token: token,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
      }
    })
  },
  oncall_alarm:function(e){
    if (e.detail.value) {
      page_call+=1;
    }
    else{
      page_call -= 1;
    }
    //console.log('oncall_alarm:', e.detail.value)
    //console.log('page_call:', page_call)
  },
   oncall_login: function (e) {
     if (e.detail.value) {
       page_call += 2;
     }
     else {
       page_call -= 2;
     }
     //console.log('oncall_login:', e.detail.value)
     //console.log('page_call:', page_call)
  },
   oncall_logout: function (e) {
     if (e.detail.value) {
       page_call += 4;
     }
     else {
       page_call -= 4;
     }
     //console.log('oncall_logout:', e.detail.value)
     //console.log('page_call:', page_call)
  },

})